<?php


class TrainingController extends CSupervisorController {

    private $_model;
    public $pageTitle = "Latihan";

    /**
     * 
     */
    public function actionIndex(){
        $dataProvider = new CActiveDataProvider('Training', array(
            'pagination' => array(
                'pageSize' => 20,
            ),
        ));

        $this->render('index' , array('dataProvider' => $dataProvider));
    }

    /**
     * 
     */
    public function actionView(){
        $model = $this->loadModel();
        $this->pageTitle = $model->name;
        $chapters = $model->getChapters();

        foreach ($chapters as $chap) {
            //echo $chap->name . " " . $chap->previousChapter->name . " " . $chap->training->name . "<br />";
            //echo $chap->id . " " . $chap->getRequiredChapter()->id . "<br />";
            //echo $chap->name . " " . $chap->isAccessible(Yii::app()->user , $model->id) . "<br />";
        }

        $this->render('view' , array('model' => $model , 'chapters' => $chapters));
    }

    /**
     * 
     */
    public function actionDelete() {
        $model = $this->loadModel();
        $model->delete();
        if (!isset($_GET['ajax']))
            $this->redirect(array('index'));
    }

    public function actionUpdate() {
        $model = $this->loadModel();
        $model->setScenario('update');
        if (isset($_POST['Training'])) {
            $model->setAttributes($_POST['Training']);
            if ($model->validate()) {
                $model->save(false);
                $this->redirect(array('update', 'id' => $model->id));
            }
        }

        $this->render('update' , array('model' => $model));
    }

    /**
     * 
     */
    public function actionCreate() {
        $model = new Training('create');

        if (isset($_POST['Training'])) {
            $model->creator_id = Yii::app()->user->id;
            $model->setAttributes($_POST['Training']);
            if ($model->validate()) {
                $model->save(false);
                $this->redirect(array('update', 'id' => $model->id));
            }
        }

        $this->render('create', array('model' => $model));
    }

    //

    public function actionAddChapter(){
        //ajax
    }

    public function actionRemoveChapter(){
        //ajax
    }

    //
    /**
     * 
     */
    public function actionPublish(){
        //ajax
    }

    public function actionUnpublish(){
        //ajax
    }

    //
    public function actionChapterLookup(){
        if (Yii::app()->request->isAjaxRequest && isset($_GET['term'])){
            $title = $_GET['term'];
            $criteria = new CDbCriteria;
            $criteria->condition = "name LIKE :sterm";
            $criteria->params = array(":sterm" => "%$title%");
            $problems = Chapter::model()->findAll($criteria);
            $retval = array();
            foreach ($problems as $problem) {
                if ($chapter->nextChapter === NULL && $chapter->previousChapter === NULL && $chapter->training === NULL) {
                    $retval[] = array(
                        'value' => $problem->getAttribute('id'),
                        'label' => $problem->getAttribute('id') . '. ' . $problem->getAttribute('name'),
                    );
                }
            }
            echo CJSON::encode($retval);
        }
    }

    public function loadModel() {
        if ($this->_model === null) {
            if (isset($_GET['id']))
                $this->_model = Training::model()->findbyPk($_GET['id']);
            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->_model;
    }
}